package com.fastCms.business.frame.manage;

import com.alibaba.fastjson.JSON;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.fastCms.business.frame.constant.RedisConst;
import com.fastCms.business.frame.pojo.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 哈密瓜科技 版权所有 © Copyright 2019<br>
 *
 * @Description: C端用户<br>
 * @Project: hami <br>
 * @CreateDate: Created in 2019/3/7 下午3:48 <br>
 * @Author: <a href="lsquan666@163.com">lsquan</a>
 */
@Component
public class UserManage {
    public static final String USER_ID = "userId";
    public static final String ORGANIZE_CODE = "organizeCode";
    public static final String USERNAME = "username";
    private static final TransmittableThreadLocal<String> USER_TOKEN = new TransmittableThreadLocal();

    public void setUserToken(String userToken) {
        USER_TOKEN.set(userToken);
    }

    public static StringRedisTemplate stringRedisTemplate;

    @Autowired(required = true)
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 获取当前登录用户 ID
     *
     * @return
     */
    public static String getUserId() {
        HashMap<String, Object> map = getUserByToken(USER_TOKEN.get());
        if (map != null) {
            return String.valueOf(map.get(USER_ID));
        }
        return null;
    }

    /**
     * 获取当前登录用户 token
     *
     * @return
     */
    public static String getToken() {

        if (StringUtils.isBlank(USER_TOKEN.get())) {
            return null;
        }
        return USER_TOKEN.get();
    }

    /**
     * @Description 获取当前登录用户 组织编码
     * @Param []
     * @Return java.lang.String
     * @Date 2019/3/9 下午4:54 lsquan
     */
    public static String getOrganizeCode() {
        HashMap<String, Object> map = getUserByToken(USER_TOKEN.get());
        if (map != null) {
            return String.valueOf(map.get(ORGANIZE_CODE));
        }
        return null;
    }

    /**
     * @Description 获取用户个人指定信息
     * @Param [key]
     * @Return java.lang.String
     * @Date 2019/3/9 下午4:55 lsquan
     */
    public static String getUserInfo(String key) {
        String token = USER_TOKEN.get();
        if (StringUtils.isBlank(token) || StringUtils.isBlank(key)) {
            return null;
        }
        HashMap<String, Object> map = getUserByToken(token);
        return String.valueOf(map.get(key));

    }

    public static HashMap<String, Object> getUser() {
        String token = USER_TOKEN.get();
        if (StringUtils.isBlank(token)) {
            return null;
        }
        HashMap<String, Object> map = getUserByToken(token);
        return map;

    }


    /**
     * @Description //获取用户所有信息
     * @Param [token]
     * @Return java.util.HashMap
     * @Date 2019/3/9 下午4:55 lsquan
     */
    public static HashMap getUserByToken(String token) {
        if (token == null) {
            return null;
        }
        String t = stringRedisTemplate.opsForValue().get(RedisConst.USER.TOKEN + token);
        HashMap<String, Object> map = JSON.parseObject(t, HashMap.class);
        return map;
    }

    /**
     * @Description //获取用户所有信息
     * @Param [token]
     * @Return java.util.HashMap
     * @Date 2019/3/9 下午4:55 lsquan
     */
    public static UserModel getUserModelByToken(String token) {
        if (StringUtils.isBlank(token)) {
            token = "";
        }
        String t = stringRedisTemplate.opsForValue().get(RedisConst.USER.TOKEN + token);
        UserModel userModel = JSON.parseObject(t, UserModel.class);
        return userModel;
    }

    /**
     * @Description 获取用户姓名
     * @Param
     * @Return java.lang.String
     * @Date 2019/3/7 下午4:05 lsquan
     */
    public static String getUsername() {
        String username = "";
        HashMap userByToken = getUserByToken(USER_TOKEN.get());
        if (null == userByToken) {
            return "test.ftl";
        }
        if (userByToken.get("user") instanceof Map) {
            Map<String, String> map = (Map<String, String>) userByToken.get("user");
            username = map.get("USERNAME");
        }
        return username;
    }
}
